<template>
  <div class="xuqiu">
    <Breadcrumb :breadStr="breadStr"></Breadcrumb>
    <!-- 搜索框、和发布招聘的按钮，包含发布招聘的对话框 -->
    <el-card
      shadow="hover"
      class="search-and-add"
      :body-style="{ padding: '15px' }"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入搜索内容，支持部门和岗位名称模糊查询"
            v-model="queryInfo.query"
            clearable
            @clear="getZpList"
            @keyup.enter.native="getZpList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getZpList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            @click="showDialog()"
          >发布招聘</el-button>
          <!-- TODO 点击发布招聘后，要清空记录值 -->
        </el-col>
      </el-row>
      <!-- 发布招聘信息的对话框，弹出层 -->
      <!-- 通过vuex传值，所以没有进行组件间传值 -->
      <ZhaopinDetail></ZhaopinDetail>
    </el-card>
    <!-- 展示所有招聘信息的列表 -->
    <el-card
      shadow="hover"
      class="information-list"
      :body-style="{ padding: '20px' }"
    >
      <el-table
        :data="zpList"
        border
        stripe
        :default-sort="{prop: 'date', order: 'descending'}"
        style="width:100%"
      >
        <template slot="empty">
          <p>暂未发布任何招聘</p>
        </template>
        <el-table-column
          prop="bumen"
          label="部门"
          sortable
        >
        </el-table-column>
        <el-table-column
          prop="gangwei"
          label="招聘岗位名称"
          sortable
        >
        </el-table-column>
        <el-table-column
          prop="num"
          label="招聘人数"
          sortable
        >
        </el-table-column>
        <el-table-column
          prop="shiyou"
          label="申请事由"
          sortable
        >
        </el-table-column>
        <el-table-column
          prop="data"
          sortable
          label="发布日期"
        >
        </el-table-column>
        <el-table-column label="编辑">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >详情/修改</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="delZp(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 下面分页显示的条条 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        :current-page="queryInfo.pagenum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { homeMixin } from '../../utils/mixin'
import Breadcrumb from '../common/Breadcrumb'
import ZhaopinDetail from '../common/ZhaopinDetail'
export default {
  mixins: [homeMixin],
  components: {
    Breadcrumb, ZhaopinDetail
  },
  data () {
    return {
      zpList: [],
      total: 0,
      queryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 5
      }
    }
  },
  created () {
    this.getBread()
    this.getZpList()
  },
  methods: {
    // 点击发布招聘，显示录入招聘信息的对话框
    showDialog () {
      // 是否新增状态设置true，代表新增招聘
      this.setIsAdd(true)
      // 置为默认值
      this.setZhaopinForm({ data: new Date().format('yyyy-MM-dd'), num: 0 })
      this.setAddZpDialogVisible(true)
    },
    // 查看/修改招聘详情
    handleEdit (index, row) {
      console.log(index, row)
      this.setIsAdd(false)
      this.setZhaopinForm(row)
      this.setAddZpDialogVisible(true)
    },
    // pagesize改变事件,每页显示条数更改
    handleSizeChange (newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getZpList()
    },
    // pagenum改变事件，当前页码修改
    handleCurrentChange (newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getZpList()
    },
    // 获取招聘列表信息
    async getZpList () {
      const { data } = await this.$http.get('/zp/getList',
        {
          params:
          {
            pagenum: this.queryInfo.pagenum,
            pagesize: this.queryInfo.pagesize,
            query: this.queryInfo.query
          }
        })
      if (data.error_code === 0) {
        if (data.count > 0) {
          console.log(data.data)
          this.zpList = data.data
          this.total = data.count
          // 处理时间格式字符串
          this.zpList = this.dealTime(JSON.stringify(this.zpList), 'data')
        } else {
          console.log('没有发布任何招聘需求')
        }
      } else {
        console.log('数据获取失败')
      }
    },
    // 根据id删除单条招聘详情
    delZp (index, row) {
      this.$MessageBox.confirm('此操作将永久删除该记录，是否继续？', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        const { data } = await this.$http.post('/zp/del', { zpid: row.id })
        if (data.error_code === 0) {
          this.$message.success('删除成功')
          this.zpList = this.zpList.filter(item => {
            return item.id !== row.id
          })
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/global";
.xuqiu {
  .search-and-add {
    margin-bottom: $elcardbottom;
  }
  .information-list {
  }
  .el-pagination {
    margin-top: 20px;
  }
}
</style>
